/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0  2011-1-10 下午01:13:22
 *
 * WordAnalyzer.java
 *
 *******************************************************************/
package com.transer.engine.search;

import java.io.IOException;
import java.io.StringReader;
import java.net.URISyntaxException;

import net.paoding.analysis.analyzer.PaodingAnalyzer;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;

/**
 * class defination are there
 * 
 * @author Mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public final class WordAnalyzer {
	private static Analyzer instance;

	private WordAnalyzer() {
	}

	public static Analyzer getAnalyzer() {
		synchronized (WordAnalyzer.class) {
			if (instance == null) {
				instance = new PaodingAnalyzer();
			}
		}
		return instance;
	}

	public static void main(String[] args) throws URISyntaxException {
		getAnalyzer();
		// System.out.println(wordSplit("中华人民,共和国我是一条材"));
		// System.out.println(wordSplit("我是一条材"));
		// System.out.println(wordSplit("老美就是牛啊我是一条材"));
		// System.out.println(wordSplit("如果不充钱,GM会T你下线"));
		// System.out.println(wordSplit("欢迎来到哆啦大冒险"));
	}

	public static String encodeChinese(String word) {
		try {
			if (StringUtils.isEmpty(word)) {
				return "";
			}
			// 取得Token流
			TokenStream stream = instance.tokenStream("", new StringReader(word));
			// 重置到流的开始位置
			stream.reset();
			// 添加工具类
			TermAttribute termAtt = (TermAttribute) stream.addAttribute(TermAttribute.class);
			// OffsetAttribute offAtt = (OffsetAttribute)
			// stream.addAttribute(OffsetAttribute.class);
			Object[] words = new Object[] {};
			// 循环打印所有分词及其位置
			while (stream.incrementToken()) {
				// System.out.println(termAtt.term());
				words = ArrayUtils.add(words, termAtt.term().hashCode());
			}
			return StringUtils.join(words, " ");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
}
